/**
 * Created by mhy on 2018/6/30.
 */
import * as API from '../../api'
import * as types from '../mutation-types'

const material = {
  state: {
    frameList: [],
    frameCount: 0,
    detailFrame: null,
    infoFrame: null,
    uploadFrameResult: null,

    bgList: [],
    bgCount: 0,
    infoBg: null,
    detailBg: null,
    uploadBgResult: null,

    matcardList: [],
    matcardCount: 0,
    detailMatcard: null,
    infoMatcard: null,
    uploadMatcardResult: null,

    senceList: [],
    senceCount: 0,
    infoSence: null,
    detailSence: null,
    uploadSenceResult: null
  },

  mutations:{

    [types.INFO_FRAME] (state,data){
      state.frameList = data.list,
      state.frameCount = data.total
    },
    [types.DETAIL_FRAME] (state,data){
      state.detailFrame = data
    },
    [types.UPLOAD_FRAME] (state,data){
      state.uploadFrameResult  = data
    },
    [types.UPLOAD_BACKGROUND] (state,data){
      state.uploadBgResult = data
    },
    [types.DETAIL_BACKGROUND] (state,data){
      state.detailBg = data
    },
    [types.LIST_BACKGROUND] (state,data){
      state.bgList = data.list
      state.bgCount = data.total
    },
    [types.UPLOAD_MATCARD] (state,data){
      state.uploadMatcardResult = data
    },
    [types.DETAIL_MATCARD] (state,data){
      state.detailMatcard = data
    },
    [types.LIST_MATCARD] (state,data){
      state.matcardList = data.list
      state.matcardCount = data.total
    },
    [types.UPLOAD_SENCE] (state,data){
      state.uploadSenceResult = data
    },
    [types.DETAIL_SENCE] (state,data){
      state.detailSence = data
    },
    [types.LIST_SENCE] (state,data){
      state.senceList = data.list
      state.senceCount = data.total
    }
  },
  getters: {

  },
  actions: {
    //获得框条列表
    getFrameList ({commit,state,rootState},params){
      var prom = API.infoFrame(params)
      prom.then(function (data) {
        commit(types.INFO_FRAME, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    getFrameDetail ({commit,state,rootState},params){
      var prom = API.infoFrame(params)
      prom.then(function (data) {
        commit(types.DETAIL_FRAME, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    //上传框条
    uploadFrame ({commit,state,rootState},formdata){
      var prom = API.uploadFrame(formdata)
      prom.then(function (data) {
        state.uploadFrameResult = data
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    updateFrame  ({commit,state,rootState},formdata){
      var prom = API.infoFrame(formdata)
      prom.then(function (data) {
        state.uploadFrameResult = data
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    //上传bg
    uploadBackground({commit,state,rootState},formdata){
      var prom = API.uploadBackgroud(formdata)
      prom.then(function (data) {
        state.uploadBgResult = data
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    getBgDetail ({commit,state,rootState},params){
      var prom = API.infoBackground(params)
      prom.then(function (data) {
        commit(types.DETAIL_BACKGROUND, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    getBgList ({commit,state,rootState},params){
      var prom = API.infoBackground(params)
      prom.then(function (data) {
        commit(types.LIST_BACKGROUND, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    updateBg ({commit,state,rootState},params){
      var prom = API.infoBackground(params)
      prom.then(function (data) {
        state.infoBg = data
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    uploadMatcard({commit,state,rootState},formdata){
      var prom = API.uploadMatCard(formdata)
      prom.then(function (data) {
        state.uploadMatcardResult = data
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    getMatcardDetail ({commit,state,rootState},params){
      var prom = API.infoMatCard(params)
      prom.then(function (data) {
        commit(types.DETAIL_MATCARD, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    getMatcardList ({commit,state,rootState},params){
      var prom = API.infoMatCard(params)
      prom.then(function (data) {
        commit(types.LIST_MATCARD, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    updateMatcard ({commit,state,rootState},params){
      var prom = API.infoMatCard(params)
      prom.then(function (data) {
        state.infoMatcard = data
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    //sence
    uploadSence({commit,state,rootState},formdata){
      var prom = API.uploadSence(formdata)
      prom.then(function (data) {
        state.uploadSenceResult = data
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    getSenceDetail ({commit,state,rootState},params){
      var prom = API.infoSence(params)
      prom.then(function (data) {
        commit(types.DETAIL_SENCE, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    getSenceList ({commit,state,rootState},params){
      var prom = API.infoSence(params)
      prom.then(function (data) {
        commit(types.LIST_SENCE, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    updateSence ({commit,state,rootState},params){
      var prom = API.infoSence(params)
      prom.then(function (data) {
        state.infoSence = data
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },


    // //登录
    // loginWithPhone({commit,state,rootState},{userphone,code,codeid}) {
    //   var prom = API.loginWithPhone(userphone,code,codeid)
    //   prom.then(function (data) {
    //     state.loginresult = data
    //     rootState.reqError = null
    //   })
    //     .catch(function (err) {
    //       rootState.reqError = err
    //     })
    //     .finally(function () {
    //       rootState.reqShake++
    //     })
    //   return prom
    // },
    // //用户信息
    // getUserInfo({commit,state,rootState},{userid}){
    //   var prom = API.getUserInfo(userid)
    //   prom.then(function (data) {
    //     state.userinforesult = data
    //     rootState.reqError = null
    //   })
    //     .catch(function (err) {
    //       rootState.reqError = err
    //     })
    //     .finally(function () {
    //       rootState.reqShake++
    //     })
    //   return prom
    // }
    //
    //
    //
    //
  }
}

export default material
